using BootstrapBlazor.Components;
using JinShun.OA.UI.Auth;
using JinShun.OA.UI.Dtos;
using JinShun.OA.UI.Urls;

namespace JinShun.OA.UI.Components.Pages.Account
{
    public partial class Login
    {
        public string username { get; set; }
        public string password { get; set; }
        public string code { get; set; }
        public string imgurl { get; set; }
        public string uuid { get; set; }


        protected override void OnInitialized()
        {
            GetCodeImg();//登陆界面加载后显示验证码
            base.OnInitialized();
        }

        private async void GetCodeImg()//点击刷新验证码
        {
            CaptchaImageDto result = await http.GetAsync<CaptchaImageDto>(UrlBases.LoginGetValidationImage_Reques, null);
            string ret = Convert.ToBase64String(result.Img);
            imgurl = "data:image/gif;base64," + ret;
            uuid = result.Uuid.ToString();
            StateHasChanged();
        }
        private async Task DoLogin()
        {
            LoginInputDto vo = new LoginInputDto();
            vo.Uuid = uuid;
            vo.UserName = username;
            vo.Password = password;
            vo.Code = code;

            //判断是否管理员
            var loginFailed = await ((AntSKAuthProvider)AuthenticationStateProvider).SignIn(vo);
            if (loginFailed.GetRequesCode() == 0)
            {
                NavigationManager.NavigateTo("/");
                return;
            }
            else
            {
                NavigationManager.NavigateTo("/Account/Login");

                List<RequesError> re = loginFailed.GetData<List<RequesError>>();
                await MessageService.Show(new MessageOption()
                {
                    Content = re[0].error.message
                });

            }
        }

    }
}